package com.stylefeng.guns.modular.drugs.controller;


import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.common.persistence.dao.DrugsMapper;
import com.stylefeng.guns.common.persistence.model.Drugs;
import com.stylefeng.guns.common.persistence.model.DrugsType;
import com.stylefeng.guns.common.redis.RedisUtils;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.drugs.dao.DrugsDao;
import com.stylefeng.guns.modular.drugstype.dao.DrugstypeDao;
import com.stylefeng.guns.modular.drugstype.service.IDrugstypeService;
import com.stylefeng.guns.modular.system.warpper.DrugsWarpper;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * drugs控制器
 *
 * @author fengshuonan
 * @Date 2019-07-11 09:26:14
 */
@Controller
@RequestMapping("/drugs")
public class DrugsController extends BaseController {
    @Resource
    private DrugsMapper drugsMapper;
    @Resource
    private DrugsDao drugsDao;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private DrugstypeDao drugstypeDao;
    @Resource
    private IDrugstypeService drugstypeService;

    private String PREFIX = "/drugs/drugs/";

    /**
     * 跳转到drugs首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "drugs.html";
    }

    /**
     * 跳转到添加drugs
     */
    @RequestMapping("/drugs_add")
    public String drugsAdd() {
        return PREFIX + "drugs_add.html";
    }

    /**
     * 跳转到修改drugs
     */
    @RequestMapping("/drugs_update/{id}")
    public String drugsUpdate(@PathVariable Integer id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Drugs drugs =drugsMapper.selectById(id);
        model.addAttribute(drugs);
        model.addAttribute("typename", ConstantFactory.me().getDrugsTypeName(drugs.getTypeid()));
        LogObjectHolder.me().set(drugs);
        return PREFIX + "drugs_edit.html";
    }
    /**
     * 获取drugs列表
     */
    @ApiOperation("获取药品列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition) {
        String name = condition;
        List<Drugs> list = this.drugsDao.search(name);
        return super.warpObject(new DrugsWarpper(list));
    }

    /**
     * 获取药品类型的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.drugsDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 新增
     */
    @ApiOperation("新增药品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(@ModelAttribute @Valid Drugs drugs) {
        Date  date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String buytime = sdf.format(date);
        drugs.setBuytime(buytime);
        /**缓存redis
         *
         */
        //redisUtils.set("drugs",drugs);
         drugsMapper.insert(drugs);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除drugs
     */
    @ApiOperation("删除药品")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam(required = false) Integer id) {
        drugsMapper.deleteById(id);
        return SUCCESS_TIP;
    }


    /**
     * 修改drugs
     */
    @ApiOperation("修改药品")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@ModelAttribute @Valid Drugs drugs) {
        if (ToolUtil.isOneEmpty(drugs, drugs.getId())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Drugs drugs1 = this.drugsMapper.selectById(drugs.getId());
        Date  date =new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String updatime = sdf.format(date);
        drugs1.setName(drugs.getName());
        drugs1.setEffect(drugs.getEffect());
        drugs1.setPrice(drugs.getPrice());
        drugs1.setTypeid(drugs.getTypeid());
        drugs1.setUpdatetime(updatime);
        drugsMapper.updateById(drugs1);
        return super.SUCCESS_TIP;
    }

    /**
     * drugs详情
     */
    @ApiOperation("药品信息")
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.GET)
    public Object detail(@PathVariable Integer id, Model model) {
        if (ToolUtil.isEmpty(id)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Drugs drugs = this.drugsMapper.selectById(id);
        DrugsType drugsType=this.drugstypeService.getDrugsById(id);
        if(drugs==null && drugsType==null){
            return IF_TIP.getMessage();
        }else{
            model.addAttribute(drugs);
            model.addAttribute(drugsType);
            LogObjectHolder.me().set(drugsType);
            LogObjectHolder.me().set(drugs);
            return PREFIX + "drugs_info.html";
        }

    }
}
